﻿using LuShanTea.Admin.Web.App_Start;
using LuShanTea.Admin.Web.Models;
using LuShanTea.DTO;
using LuShanTea.IService;

using LuShanTea.Web.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LuShanTea.Admin.Web.Controllers
{
    public class RoleController : Controller
    {
        public IRoleService RoleService { get; set; }

        public IPermissionService PermissionService { get; set; }


        [CheckPermission("Role.List")]
        // GET: Role
        public ActionResult List()
        {
            RoleDTO[] roles = RoleService.GetAll();

            return View(roles);
        }


        [CheckPermission("Role.Add")]
        public ActionResult AddNew()
        {
            var permissions = PermissionService.GetAll();
            return View(permissions);
        }


        [CheckPermission("Role.Add")]
        [HttpPost]
        public ActionResult AddNew(RoleAddNewModel model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            var role = RoleService.GetByName(model.Name);

            if (role != null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = model.Name + "角色已存在" });
            }

            long roleId = RoleService.AddNew(model.Name);

            PermissionService.AddPermissionByRole(roleId, model.PermissionIds);

            return Json(new AjaxResult { Status = "ok" });
        }


        [CheckPermission("Role.Edit")]
        [HttpGet]
        public ActionResult Edit(long id)
        {
            var role = RoleService.GetById(id);

            if (role == null)
            {
                return Content("未找到ID为" + id + "的角色");
            }

            var permissions = PermissionService.GetAll();

            RoleEditGetModel model = new RoleEditGetModel();

            model.Role = role;
            model.Permissions = permissions;
            model.RolePermissions = PermissionService.GetByRoleId(id);

            return View(model);
        }


        [CheckPermission("Role.Edit")]
        [HttpPost]
        public ActionResult Edit(RoleEditModel model)
        {
            //验证数据
            if (!ModelState.IsValid)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = MVCHelper.GetValidMsg(ModelState) });
            }

            //获取数据判断角色是否存在
            var role = RoleService.GetById(model.RoleId);

            if (role == null)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = "未找到ID为" + model.RoleId + "角色,请检查是否已被删除！" });
            }

            //获取名称判断修改的角色是否已存在并且不为自身原有名称
            var dataRole = RoleService.GetByName(model.Name);

            if (dataRole != null && model.RoleId != dataRole.Id)
            {
                return Json(new AjaxResult { Status = "error", ErrorMsg = model.Name + "已存在" });
            }

            RoleService.Update(model.RoleId, model.Name);
            PermissionService.UpdatePermissionByRoleId(model.RoleId, model.PermissionIds);

            return Json(new AjaxResult { Status = "ok" });
        }


        [CheckPermission("Role.Delete")]
        [HttpPost]
        public ActionResult Delete(long roleId)
        {
            RoleService.MarkDeleted(roleId);

            return Json(new AjaxResult { Status = "ok" });
        }


        [CheckPermission("Role.Delete")]
        [HttpPost]
        public ActionResult BatchDel(long[] roleIds)
        {
            foreach (var roleId in roleIds)
            {
                RoleService.MarkDeleted(roleId);
            }

            return Json(new AjaxResult { Status = "ok" });
        }
    }
}